package com.lfec.dividiraconta;

import java.math.BigDecimal;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TextView;
import android.widget.Toast;

public class DividirMainActivity extends ActionBarActivity {

	private ListView pessoaListView;
	private ListView itemListView;
	private ArrayAdapter<Pessoa> pessoaAdapter;
	private ArrayAdapter<Item> itemAdapter;

	private TextView totalValorItens;
	private TextView totalValorItens10;

	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dividir_main);

		final View innerPessoa = LayoutInflater.from(this).inflate(
				R.layout.activity_pessoa_tab, null);
		final View innerItem = LayoutInflater.from(this).inflate(
				R.layout.activity_item_tab, null);

		tabHost = (TabHost) findViewById(android.R.id.tabhost);

		tabHost.setup();

		View view = LayoutInflater.from(this).inflate(R.layout.tab_box, null);
		TextView tv = (TextView) view.findViewById(R.id.txView);
		tv.setText(getString(R.string.pessoa_tab));
		tabHost.addTab(tabHost.newTabSpec("ITEM").setIndicator(view)
				.setContent(new TabContentFactory() {

					@Override
					public View createTabContent(String tag) {
						// TODO Auto-generated method stub
						return innerPessoa;
					}
				}));

		View view2 = LayoutInflater.from(this).inflate(R.layout.tab_box, null);
		TextView tv2 = (TextView) view2.findViewById(R.id.txView);
		tv2.setText(getString(R.string.item_tab));
		tabHost.addTab(tabHost.newTabSpec("ITEM").setIndicator(view2)
				.setContent(new TabContentFactory() {

					@Override
					public View createTabContent(String tag) {
						// TODO Auto-generated method stub
						return innerItem;
					}
				}));

		pessoaListView = (ListView) innerPessoa
				.findViewById(R.id.mainPessoaList);
		pessoaAdapter = new PessoaMainAdapter(this);

		pessoaListView.setAdapter(pessoaAdapter);

		totalValorItens = (TextView) innerItem.findViewById(R.id.itemTotal);
		totalValorItens10 = (TextView) innerItem.findViewById(R.id.itemTotal10);
		itemListView = (ListView) innerItem.findViewById(R.id.mainItemList);
		itemAdapter = new ItemMainAdapter(this);

		itemListView.setAdapter(itemAdapter);
		
		cleanAndReinstall();

	}

	@Override
	protected void onResume() {
		super.onResume();
		cleanAndReinstall();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		cleanAndReinstall();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.dividir_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.menuLimpar) {

			DadosHolder.getInstance().clear();
			cleanAndReinstall();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void cleanAndReinstall() {
		pessoaAdapter.clear();
		itemAdapter.clear();

		for (Pessoa p : DadosHolder.getInstance().getPessoaList()) {
			pessoaAdapter.add(p);
		}

		BigDecimal itemTotal = new BigDecimal("0");
		for (Item i : DadosHolder.getInstance().getItemList()) {
			itemAdapter.add(i);
			itemTotal = itemTotal.add(BigDecimal.valueOf(i.getValorTotal()));
		}

		totalValorItens.setText(itemTotal.toString());
		totalValorItens10.setText(itemTotal
				.multiply(new BigDecimal("1.1"))
				.setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
	}

	public void novaPessoa(View view) {
		startActivity(new Intent(this, NovaPessoaActivity.class));
	}

	public void novoItem(View view) {
		if (DadosHolder.getInstance().getPessoaList().size() == 0) {
			Toast.makeText(this, R.string.pessoaVazio, Toast.LENGTH_SHORT)
					.show();
			;
		} else {
			startActivity(new Intent(this, NovoProdutoActivity.class));
		}

	}
}
